package models


//查询
func QueryLogUserCount(is_super bool,cur_user string) int{
	data_list := [] LogUser{}
	var count int
	if is_super{
		db.Order("created_at desc").Find(&data_list).Count(&count)
	}else{
		db.Order("created_at desc").Where("user_name = ?",cur_user).Find(&data_list).Count(&count)
	}
	return count
}

//查询
func PageLogUserAll(page int,is_super bool,cur_user string) ([] LogUser) {
	data_list := [] LogUser{}
	if is_super{
		db.Order("created_at desc").Limit(12).Offset(12*(page-1)).Find(&data_list)
	}else{
		db.Order("created_at desc").Limit(12).Offset(12*(page-1)).Where("user_name = ?",cur_user).Find(&data_list)
	}

	return data_list
}


//添加
func AddLogUser(obj LogUser) error {
	err := db.Create(&obj).Error
	return err
}

// 搜索角色
func QueryLogUserSearch(key,cur_user string,is_super bool) []LogUser {
	data_list := [] LogUser{}
	if is_super{
		db.Order("created_at desc").Where("req LIKE ?", "%"+key+"%").
			Or("req_menu LIKE ?", "%"+key+"%").Or("created_at LIKE ?", "%"+key+"%").Limit(15).Find(&data_list)
	}else{
		db.Order("created_at desc").Where("req LIKE ? and user_name = ?", "%"+key+"%",cur_user).
			Or("req_menu LIKE ? and user_name = ?", "%"+key+"%",cur_user).Or("created_at LIKE ? and user_name = ?", "%"+key+"%",cur_user).Limit(15).Find(&data_list)
	}
	return data_list
}


//查询
func QueryLogTaskAll(cur_user string,is_super bool) [] LogTask {
	data_list := [] LogTask{}
	if is_super {
		db.Order("created_at desc").Find(&data_list)
	}else{
		db.Order("created_at desc").Where("user_name = ?",cur_user).Find(&data_list)
	}
	return data_list
}

//查询
func QueryLogTaskPage(page int,cur_user string,is_super bool) [] LogTask {
	data_list := [] LogTask{}
	if is_super{
		db.Order("created_at desc").Limit(12).Offset(12*(page-1)).Find(&data_list)
	}else{
		db.Order("created_at desc").Limit(12).Offset(12*(page-1)).Where("user_name = ?",cur_user).Find(&data_list)
	}

	return data_list
}


//添加
func AddTask(obj LogTask) error {
	err := db.Create(&obj).Error
	return err
}

func UpdateTask(id int,task LogTask) error {
	obj := LogTask{}
	err := db.First(&obj,id).Update(&task).Error
	return err
}


// 搜索
func QueryTaskSearch(key,cur_user string,is_super bool) []LogTask {
	data_list := [] LogTask{}
	if is_super{
		db.Where("task_name LIKE ?", "%"+key+"%").Or("status LIKE ?", "%"+key+"%").Or("created_at LIKE ?", "%"+key+"%").Find(&data_list)
	}else{
		db.Where("task_name LIKE ? and user_name = ?", "%"+key+"%",cur_user).Or("status LIKE ? and user_name = ?", "%"+key+"%",cur_user).
			Or("created_at LIKE ? and user_name = ?", "%"+key+"%",cur_user).Find(&data_list)
	}

	return data_list
}

//添加
func GetTaskId(id int) LogTask {
	obj := LogTask{}
	db.First(&obj,id)
	return obj
}
